#! /usr/bin/python
# _*_ coding: utf-8 _*_

'a mod with Class Maps'

__author__ = 'alanpo'

import blocks, enums
import random, os, time


class Maps:
    def __init__(self, x, y):
        self.size = [x, y]  #地图尺寸
        self.headloc = [0, 0]  #蛇头位置
        self.tailloc = [0, 0]  #蛇尾位置
        self.level = 1  #游戏等级
        self.score = 0  #得分
        self.speed = 300  #速度值
        self.welcome = '欢迎进入贪吃蛇游戏,游戏马上开始!'
        self.turn = 0
        #初始化pixel
        self.pixel = []
        for xx in range(x):
            _pixel = []
            for yy in range(y):
                _pixel.append(blocks.Pixel(xx, yy, x, y))
            self.pixel.append(_pixel)
        #初始化边框
        for xx in range(x):
            self.pixel[xx][0].set_framel()
            self.pixel[xx][y - 1].set_framer()
        for yy in range(y):
            self.pixel[0][yy].set_framexy()
            self.pixel[x - 1][yy].set_framexy()

    #创建初始的蛇
    def set_snake(self, long: int):
        x = random.randint(long, self.size[0] - long)
        y = random.randint(long, self.size[1] - long)  #随机蛇尾位置
        way = enums.to_direction(random.randint(1, 4))  #随机移动方向
        self.tailloc = [x, y]
        self.pixel[x][y].set_tail(way)  #初始化为蛇尾
        loc = self.pixel[x][y].follow  #找到下一个位置
        for _ in range(2, long):
            self.pixel[loc[0]][loc[1]].set_body(way)  #初始化为蛇身
            loc = self.pixel[loc[0]][loc[1]].follow  #找到下一个位置
        self.pixel[loc[0]][loc[1]].set_head(way)  #初始化为蛇头
        self.headloc = loc
        return self

    #放置果实
    def put_food(self, num):
        while True:
            if num == 0: break
            x = random.randint(1, self.size[0] - 1)
            y = random.randint(1, self.size[1] - 1)  #随机果实位置
            if self.pixel[x][y].iswhat == enums.IsWhat.Block:
                self.pixel[x][y].set_food()
                num -= 1
        return self

    #获取转向参数
    def do_turn(self, turn):
        self.turn = turn

    #获取输入并执行转向
    def judge_to_turn(self):
        now = self.pixel[self.headloc[0]][
            self.headloc[1]].nextway.get_direction()
        new = self.turn
        #判断蛇头是左(右)转并设置新的移动方向
        if abs(abs(now - new + 1) - 2) != 1 and new != 0:
            self.pixel[self.headloc[0]][self.headloc[1]].set_way(
                enums.to_direction(new))
            # self.turn = 0
        return self

    #判断移动后状态
    def judge_if_move(self):
        loc = self.pixel[self.headloc[0]][self.headloc[1]].follow
        if self.pixel[loc[0]][loc[1]].iswhat == enums.IsWhat.Block:
            return enums.Todo.Move
        elif self.pixel[loc[0]][loc[1]].iswhat == enums.IsWhat.Food:
            return enums.Todo.Grow
        else:
            return enums.Todo.Die

    #执行移动
    def do_move(self):
        do = self.judge_if_move()
        if do == enums.Todo.Die:
            return True
        elif do == enums.Todo.Move:
            self.move_head().move_tail()
            return False
        else:
            self.move_head().put_food(1).score += 1
            self.level = self.score // 50 + 1
            self.speed = self.speed / self.level
            return False

    #移动蛇头
    def move_head(self):
        loc = self.headloc
        way = self.pixel[loc[0]][loc[1]].nextway
        self.pixel[loc[0]][loc[1]].to_body()  #头变身
        self.headloc = self.pixel[loc[0]][loc[1]].follow  #定位下一个蛇头
        self.pixel[self.headloc[0]][self.headloc[1]].set_head(way)  #变蛇
        return self

    #移动蛇尾
    def move_tail(self):
        loc = self.tailloc
        self.tailloc = self.pixel[loc[0]][loc[1]].follow  #定位下一个蛇尾
        self.pixel[loc[0]][loc[1]].set_block()  #尾变空
        self.pixel[self.tailloc[0]][self.tailloc[1]].to_tail()  #身变尾
        return self

    #显示欢迎词
    def show_welcome(self):
        os.system('echo -e "\033[H"')  #光标位置回原点
        os.system('stty cooked')  #终端行模式
        for i in range(self.size[0] // 2):
            print()
        for j in range(self.size[1] // 2):
            print(end=' ')
        print(self.welcome)
        os.system('stty raw')
        return self

    #显示地图
    def show(self):
        os.system('echo -e "\033[H"')  #光标位置回原点
        os.system('stty cooked')  #终端行模式
        for i in range(self.size[0]):
            for j in range(self.size[1]):
                self.pixel[i][j].prt()
            print()
        print("W:↑, S:↓, A:←, D:→, Q:quit, Level: {}   Score: {},  转向值: {}".
              format(self.level, self.score, self.turn))
        os.system('stty raw')
        return self

    #休眠
    def sleep(self):
        time.sleep(self.speed / 600)
        return self


#取绝对值
def abs(x):
    if x < 0:
        return -x
    else:
        return x


#test
if __name__ == "__main__":
    x = Maps(22, 52)
    x.set_snake(5).put_food(3).show_welcome().sleep().show()